package activity

import (
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model/activity"
	"gq_volunteer_platform/model/user"
)

// 活动到期结束
// 用户加入积分
func ActivityFinish(act activity.Activity) (err error) {

	var two activity.Activity
	two.ID = act.ID
	two.Status = "已结束"

	var tx = bean.MysqlDb.Begin()

	// 修改活动状态
	if err := tx.Updates(&two).Error; err != nil {

		tx.Rollback()
		return err
	}

	var actUserList []activity.ActivityUser

	// 该活动下的用户 已审核状态
	if err := bean.MysqlDb.Model(&activity.ActivityUser{}).
		Where("`activity_id`=? AND `check_status`=? AND `sign_status`=?", act.ID, "已审核", "已签到").
		Find(&actUserList).Error; err != nil {

		tx.Rollback()
		return err
	}

	// 修改用户积分
	for _, item := range actUserList {
		if err := user.AddPoint(tx, item.UserId, int(act.ServeHour), "参加活动："+act.Name); err != nil {

			tx.Rollback()
			return err
		}
	}

	// 提交执行
	if err := tx.Commit().Error; err != nil {

		tx.Rollback()
		return err
	}

	return nil
}

func ActivityStart(act activity.Activity) (err error) {

	var two activity.Activity
	two.ID = act.ID
	two.Status = "进行中"

	var tx = bean.MysqlDb.Begin()

	// 修改活动状态
	if err := tx.Updates(&two).Error; err != nil {
		tx.Rollback()
		return err
	}

	// 提交活动开始通知

	// 提交执行
	if err := tx.Commit().Error; err != nil {
		tx.Rollback()
		return err
	}

	return nil
}
